package com.shtel.paas.monitorcenter.asset.management.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.shtel.paas.monitorcenter.asset.management.entity.AlarmRange;
import com.shtel.paas.monitorcenter.asset.management.entity.HostListEntity;
import com.shtel.paas.monitorcenter.asset.management.mapper.HostInfoMapper;
import com.shtel.paas.monitorcenter.asset.management.service.HostInfoService;
import com.shtel.paas.monitorcenter.util.Page;

@Service
public class HostInfoServiceImpl implements HostInfoService {

	@Autowired
	HostInfoMapper hostInfoMapper;

	String[] hostInfoList = { "sh_region;所属域", "sh_sysname;系统名称", "sh_roomname;机房", "sh_rackname;机架",
			"location;设备存放位置", "systemname;所属应用系统", "host_used_for;主机用途", "pn;P/N", "serialon;序列号",
			"modeldesc;型号描述", "clname;配置项名称", "sh_ipaddr;IP地址", "sh_mask;掩码", "sh_ipgateway;网关", "mgraddr;管理卡地址",
			"clcategory;配置项分类", "clcode;搜索代码", "clid;配置项标识", "remark;备注", "devicename;设备名称", "serialnumber;资产编号",
			"usedstatus;使用状态", "mendfactory;维护厂家", "mendpeople;维护联系人", "mendperiod;维护有效期", "mendphone;维护联系电话",
			"region;区域", "buy_date;购置日期", "cablenetcardcount;电口网卡数量", "cbdate;出保日期", "connwithothersys;是否与其他系统共用",
			"cpuinfo;CPU情况", "cpumodel;CPU型号", "cpuspeed;CPU主频", "cputype;CPU类型", "databasever;数据库版本",
			"devicetype;品牌类型", "diskinfo;磁盘信息", "disksize;硬盘大小", "divisioncfg;分区配置", "divisionnpg;分区大小",
			"enableinterface;可用的接口卡数量", "entitycode;实物编码", "fans;风扇数量", "fc_side;FC对端", "havecddriver;是否配光驱",
			"havesubarea;是否有分区", "havetape;是否配磁带机", "hbacardamount;HBA卡数量", "hostid;主机服务器标识", "hostmodel;型号",
			"installdisks;安装的磁盘数量", "iochannel;I/O通道数量", "ipaddress;IP地址", "logiccpu;逻辑CPU数量",
			"logicsubarea;逻辑分区数量", "manufactory;生产厂商", "maxcpucount;满配CPU个数", "maxdiskcount;满配磁盘个数",
			"maxmemslotcount;满配磁盘插槽数量", "memmodel;内存型号", "memnum;内存数量", "mgrcardswitchport;管理卡交换机端口",
			"networkcardamount;网卡数量", "net_side;网络对端", "opticsnetcardinfo;光口网卡数量", "osname;操作系统",
			"outer_sys_inter;外系统接口", "paaaword;口令", "physicscpu;物理CPU数量", "physicsmenory;物理内存大小", "powercount;电源数量",
			"prjname;项目名称", "pepaired_man;维护人", "roomposition;机房位置", "san;SAN", "server_type;服务器类型",
			"switchport;交换机端口", "sw_applation;软件应用", "sw_config;软件配置", "tpcc;TPCC值", "usedmemslotcount;已用内存插槽数量",
			"user_man;使用人" };
	

	@Override
	public Page<HostListEntity> quertHostInfo(String region, String ipAddr, String roomName,
			String rackName, int page, int size) {
		PageHelper.startPage(page, size);
		Map<String, String> param = new HashMap<String, String>();
		param.put("region", region);
		param.put("ipAddr", ipAddr);
		param.put("roomName", roomName);
		param.put("rackName", rackName);
		List<HostListEntity> hostList = hostInfoMapper.quertHostInfo(param);
		Page<HostListEntity> pageContent = Page.build(hostList);
		
		return pageContent;
	}

	@Override
	public Integer daleteHostInfo(List<Integer> ids) {
		
		Integer num = hostInfoMapper.daleteHostInfo(ids);
		return num;
	}

	@Override
	public Integer updateHostInfo(Map<String, String> paramMap) {

		Integer num = hostInfoMapper.updateHostInfo(paramMap);
		return num;
		
	}

	@Override
	public Integer insertHostInfo(Map<String, String> paramMap) {
		Integer num = hostInfoMapper.insertHostInfo(paramMap);
		return num;
	}

	@Override
	public byte[] exportHostList1() {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Workbook workbook = new HSSFWorkbook();
		
		//创建工作表sheet
		Sheet sheet = workbook.createSheet("host");
		//创建第一行
		Row row=sheet.createRow(0);
		Cell cell=null;
		for(int i=0; i<hostInfoList.length; i++) {
			String str = hostInfoList[i].split(";")[1];
		    cell=row.createCell(i);
		    cell.setCellValue(str);
		}

		List<Map<String, Object>> mapList = hostInfoMapper.quertAllHostInfo();
		
		for(int i=0; i<mapList.size(); i++) {
			Row nrow=sheet.createRow(i+1);
			Map<String, Object> map = mapList.get(i);
			for(int j=0; j<hostInfoList.length; j++) {
				String str = hostInfoList[j].split(";")[0];
				Cell ncell=nrow.createCell(j);
				String value = (String) map.get(str);
				
				if(null!=value||!"".equals(value)) {
					ncell.setCellValue(value);
				}
			}
		}
		
		byte[] body = null;
		try {
			workbook.write(os);
			body = os.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return body;
		
		
	}

	@Override
	public List<Map<String, Object>> exportHostList() {
		
		List<Map<String,Object>> mapList = hostInfoMapper.quertAllHostInfo();
		
		return mapList;
		
		
	}
	
	@Override
	public List<String> getFields() {
		List<String> fieldList = new ArrayList<String>();
		
		for(String str : hostInfoList) {
			fieldList.add(str);
		}
		
		return fieldList;
	}

	@Override
	public List<Map<String, Object>> queryAlarmConfigType2(Map<String, String> param){
		
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
			result = hostInfoMapper.queryAlarmConfigType2(param);
		
		return result;
	}

	@Override
	public List<Map<String, Object>> queryAlarmConfigType1(List<AlarmRange> list, Map<String, String> param) {
		
		
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		for(AlarmRange alarmRange : list) {
			
			if(alarmRange.getRuledesc().equals(param.get("kpiname"))) {
				param.put("frequencyIds", alarmRange.getAlarm_frequency_id());
				result.addAll(hostInfoMapper.queryAlarmConfigType1(param));
			}
			
			
		}
	
	return result;
	}
}
